package cn.cloudreal.cbms.resmonitor.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.drools.lang.DRLExpressions.neg_operator_key_return;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import cn.cloudreal.cbms.common.util.Constants;
import cn.cloudreal.cbms.common.util.PageBean;
import cn.cloudreal.cbms.common.util.TimeTools;
import cn.cloudreal.cbms.resmonitor.dao.ResMonitorDao;
import cn.cloudreal.cbms.vo.FundBudget;
import cn.cloudreal.cbms.vo.ItemAccrual;
import cn.cloudreal.cbms.vo.Mediasumm;
import cn.cloudreal.cbms.vo.PutInPlanMonth;
import cn.cloudreal.cbms.vo.ResourceMonitor;
import cn.cloudreal.cbms.vo.SourcePlanYear;

/**
 * <p>
 * Title: ITC
 * </p>
 * <p>
 * Description:资源监控dao层实现类
 * </p>
 * <p>
 * Copyright: Copyright (c) 2010
 * </p>
 * <p>
 * Company:云恒瑞通
 * </p>
 * 
 * @author 夏加冬
 * @date 2012-7-9
 */
public class ResMonitorDaoImpl extends SqlMapClientDaoSupport implements
        ResMonitorDao
{
    private final String GET_ALL_MEDIASUMMS_BY_YEAR = "mediaPrice.getAllByYear";
    private final String SOURCEPLANYEAR_ADD = "sourcePlanYear.add";
    private final String PUTINPLANMONTH_ADD = "putInPlanMonth.add";
    private final String FUNDBUDGET_ADD = "fundBudget.addBudget";
    private final String SOURCEPLANYEAR_UPDATE = "sourcePlanYear.update";
    private final String PUTINPLANMONTH_UPDATE = "putInPlanMonth.update";
    private final String SOURCEPLANYEAR_SELECT = "sourcePlanYear.select";
    private final String GET_ALL_RESMONITORS_BY_CONDITION = "resourceMonit.getAllResMonitorsList";
    private final String GET_ALL_PURMONTHS_BY_CONDITION = "putInPlanMonth.getPutMonthList";
    private final String GET_ALL_COUNTS_BY_CONDITION = "resourceMonit.getAllResMonitorsCount";
    private final String GET_SOURCEPLAN = "sourcePlanYear.getSourcePlan";
    private final String GET_FUNDBUFGET = "fundBudget.getBudget";
    private final String FIND_PutMonthByNowTotalPutInAndType = "putInPlanMonth.findPutMonthByNowTotalPutInAndType";
    
    /** 日志工具类实例 */
    private static final Logger log = Logger.getLogger(ResMonitorDaoImpl.class);

    /**
     * 查询列表数据
     * 
     * @param page
     * @return List<Sample>
     */
    @SuppressWarnings("unchecked")
    public List<ResourceMonitor> getMediaSummaries(PageBean page, Map map)
    {
        // 打印进入方法体日志l
        if (log.isInfoEnabled())
        {
            log.info("Enter ResMonitorDaoImpl getMediaSummariesByYear() method,return List<Mediasumm>");
        }
        Integer count = (Integer) getSqlMapClientTemplate().queryForObject(GET_ALL_COUNTS_BY_CONDITION,
                map);
        List<ResourceMonitor> resourceMonitors = null;
        if (count != null)
        // 设置分页对象总记录数
            page.setTotalRecords(count);
        // 总记录数不为零才查询
        if (page.getTotalRecords() != 0)
        {
            // 查找资源监控数据集
            map.put("i_selecttype", 2);
            map.put("i_starid", (page.getPageNo() - 1) * page.getPageSize());
            map.put("i_endid", (page.getPageNo() - 1) * page.getPageSize()
                    + page.getPageSize());

            resourceMonitors = getSqlMapClientTemplate().queryForList(GET_ALL_RESMONITORS_BY_CONDITION,
                    map);

        }
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ResMonitorDaoImpl getMediaSummariesByYear() method,return List<Mediasumm>");
        }
        return resourceMonitors;
    }

    /**
     * 根据合同年份查询相关的媒体名称
     * 
     * @param page
     * @return List<Sample>
     */
    @SuppressWarnings("unchecked")
    public List<Mediasumm> getMediaSummariesByYear(int year,
            String mediaName,
            String city)
    {
        // 打印进入方法体日志l
        if (log.isInfoEnabled())
        {
            log.info("Enter ResMonitorDaoImpl getMediaSummariesByYear() method,return List<Mediasumm>");
        }
        log.debug(year);

        List<Mediasumm> mediasumms = null;
        Map<String, String> param1 = new HashMap<String, String>();
        String searchyear = String.valueOf(year);
        param1.put("year", searchyear);
        param1.put("effective_time", TimeTools.getStringByFormat(new Date(),"yyyy-MM-dd"));
        // 如果媒体名称不为空
        if (mediaName != null)
        {
            param1.put("mediaName", mediaName);
        }
        // 如果地市不为空
        if (city != null)
        {
            param1.put("city", city);
        }
        mediasumms = getSqlMapClientTemplate().queryForList(GET_ALL_MEDIASUMMS_BY_YEAR,
                param1);
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ResMonitorDaoImpl getMediaSummariesByYear() method,return List<Mediasumm>");
        }
        return mediasumms;
    }

    /**
     * 添加预算（年）
     */
    public int saveSourcePlanYear(SourcePlanYear sourcePlanYear){
        Integer num = (Integer) this.getSqlMapClientTemplate().insert(SOURCEPLANYEAR_ADD,sourcePlanYear);
        return num;
    }
    
    /**
     * 添加预算(月)
     */
    public void savePutInPlanMonth(PutInPlanMonth putInPlanMonth){
       this.getSqlMapClientTemplate().insert(PUTINPLANMONTH_ADD,putInPlanMonth);
    }
    
    
    /**
     * 资源监控媒体计划投放量录入
     * 
     * @param page
     * @return null
     */
    public Integer addResMonitorRelatoin(List<PutInPlanMonth> putInPlanMonths,
            SourcePlanYear sourcePlanYear,
            int m)
    {
        // 打印进入方法体日志l
        if (log.isInfoEnabled())
        {
            log.info("Enter ResMonitorDaoImpl addResMonitorRelatoin() method,return null");
        }
        Integer id = null;
        if (m == 2)
        {
            // 执行添加资源投放计划
            id  = (Integer) getSqlMapClientTemplate().insert(SOURCEPLANYEAR_ADD, sourcePlanYear);
            // 新增分阶段投放计划
            for (int i = 0; i < putInPlanMonths.size(); i++)
            {
                // 设置媒体id
                putInPlanMonths.get(i).setMediaID(sourcePlanYear.getMediaID());
                // 设置年份
                putInPlanMonths.get(i)
                        .setPlanYear(sourcePlanYear.getPlanYear());
                // 设置名称
                putInPlanMonths.get(i)
                        .setMediaName(sourcePlanYear.getMediaName());
                // 设置地市
                putInPlanMonths.get(i).setCity(sourcePlanYear.getCity());
                // 设置费用类型
                putInPlanMonths.get(i)
                        .setCostType(sourcePlanYear.getCostType());
                // 设置媒体价格id
                putInPlanMonths.get(i).setId_price(sourcePlanYear.getId_price());
                // 设置费用子类
                putInPlanMonths.get(i)
                        .setCostSubtype(sourcePlanYear.getCostSubtype());
                //设置关联的资源投放id
                putInPlanMonths.get(i).setSourceId(id);
                // 执行分阶段投放计划
                getSqlMapClientTemplate().insert(PUTINPLANMONTH_ADD,
                        putInPlanMonths.get(i));

            }
        }
        else
        {
            // 执行添加费用预算
            FundBudget fundBudget = new FundBudget();
            fundBudget.setBudget(sourcePlanYear.getBudget());
            fundBudget.setCity(sourcePlanYear.getCity());
            fundBudget.setCostSubtype(sourcePlanYear.getCostSubtype());
            fundBudget.setCostType(sourcePlanYear.getCostType());
            fundBudget.setYear(sourcePlanYear.getPlanYear());
            // 执行添加费用预算
            getSqlMapClientTemplate().insert(FUNDBUDGET_ADD, fundBudget);
        }
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ResMonitorDaoImpl addResMonitorRelatoin() method,return null");
        }
        return id;
    }

    /**
     * 资源监控媒体计划投放量录入用于导入后的修改(年)
     * 
     * @param page
     * @return null
     */
    public void addResMonitorRelatoin_01(List<PutInPlanMonth> putInPlanMonths, SourcePlanYear sourcePlanYear, int m)
    {
        // 打印进入方法体日志l
        if (log.isInfoEnabled())
        {
            log.info("Enter ResMonitorDaoImpl addResMonitorRelatoin() method,return null");
        }
        if (m == 2)
        {
            // 新增分阶段投放计划
            for (int i = 0; i < putInPlanMonths.size(); i++)
            {
                // 设置媒体id
                putInPlanMonths.get(i).setMediaID(sourcePlanYear.getMediaID());
                // 设置年份
                putInPlanMonths.get(i)
                        .setPlanYear(sourcePlanYear.getPlanYear());
                // 设置名称
                putInPlanMonths.get(i)
                        .setMediaName(sourcePlanYear.getMediaName());
                // 设置地市
                putInPlanMonths.get(i).setCity(sourcePlanYear.getCity());
                // 设置费用类型
                putInPlanMonths.get(i)
                        .setCostType(sourcePlanYear.getCostType());
                // 设置媒体价格id
                putInPlanMonths.get(i).setId_price(sourcePlanYear.getId_price());
                // 设置费用子类
                putInPlanMonths.get(i)
                        .setCostSubtype(sourcePlanYear.getCostSubtype());
                //设置关联的资源投放id
                putInPlanMonths.get(i).setSourceId(sourcePlanYear.getId());
                // 执行分阶段投放计划
                getSqlMapClientTemplate().insert(PUTINPLANMONTH_ADD,
                        putInPlanMonths.get(i));

            }
        }
        else
        {
            // 执行添加费用预算
            FundBudget fundBudget = new FundBudget();
            fundBudget.setBudget(sourcePlanYear.getBudget());
            fundBudget.setCity(sourcePlanYear.getCity());
            fundBudget.setCostSubtype(sourcePlanYear.getCostSubtype());
            fundBudget.setCostType(sourcePlanYear.getCostType());
            fundBudget.setYear(sourcePlanYear.getPlanYear());
            // 执行添加费用预算
            getSqlMapClientTemplate().insert(FUNDBUDGET_ADD, fundBudget);
        }
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ResMonitorDaoImpl addResMonitorRelatoin() method,return null");
        }
    }
    
    /**
     * 资源监控媒体计划投放量更新
     * 
     * @param page
     * @return null
     */
    public void updateResMonitorRelatoin(List<PutInPlanMonth> putInPlanMonths,
            SourcePlanYear sourcePlanYear)
    {
        // 打印进入方法体日志l
        if (log.isInfoEnabled())
        {
            log.info("Enter ResMonitorDaoImpl updateResMonitorRelatoin() method,return null");
        }
        // 执行添加资源投放计划
        getSqlMapClientTemplate().update(SOURCEPLANYEAR_UPDATE, sourcePlanYear);
        // 新增分阶段投放计划
        for (int i = 0; i < putInPlanMonths.size(); i++)
        {
            // 执行分阶段投放计划
            getSqlMapClientTemplate().update(PUTINPLANMONTH_UPDATE,
                    putInPlanMonths.get(i));

        }
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ResMonitorDaoImpl updateResMonitorRelatoin() method,return null");
        }
    }

    /**
     * 查找年度资源投放计划
     * 
     * @return SourcePlanYear
     */
    public SourcePlanYear getSourcePlanYear(Integer sourceId)
    {
        Map<String, Integer> param1 = new HashMap<String, Integer>();
        param1.put("id", sourceId);
        return (SourcePlanYear) getSqlMapClientTemplate().queryForObject(SOURCEPLANYEAR_SELECT,
                param1);
    }

    /**
     * 获取分阶段投放计划
     * 
     * @return list
     */
    @SuppressWarnings("unchecked")
    public List<PutInPlanMonth> getPutInPlanMonths(String idPrice,
            String costType,
            String costSubType)
    {
        List<PutInPlanMonth> putInPlanMonths = null;
        Map<String, String> param1 = new HashMap<String, String>();
        param1.put("idPrice", idPrice);
        param1.put("costType", costType);
        param1.put("costSubType", costSubType);
        putInPlanMonths = getSqlMapClientTemplate().queryForList(GET_ALL_PURMONTHS_BY_CONDITION,
                param1);
        return putInPlanMonths;
    }

    /**
     * 获取年度投放计划
     * 
     * @return list
     */
    public SourcePlanYear getSourceIsExist(String id_price,
            String costType,
            String costSubType)
    {
        SourcePlanYear sourcePlanYear = new SourcePlanYear();
        sourcePlanYear.setId_price(Integer.valueOf(id_price));
        sourcePlanYear.setCostType(costType);
        sourcePlanYear.setCostSubtype(costSubType);
        SourcePlanYear sourcePlanYear2 = (SourcePlanYear) getSqlMapClientTemplate().queryForObject(GET_SOURCEPLAN,
                sourcePlanYear);
        return sourcePlanYear2;
    }
    /**
     * 获取费用预算
     * @return list
     */
    public FundBudget getFundBudgetIsExist(String year,String city,String costType,String costSubType)
    {
        FundBudget fundBudget = new FundBudget();
        fundBudget.setYear(year);
        fundBudget.setCostSubtype(costSubType);
        fundBudget.setCostType(costType);
        fundBudget.setCity(city);
        FundBudget fundBudget2 = (FundBudget) getSqlMapClientTemplate().queryForObject(GET_FUNDBUFGET,
                fundBudget);
        return fundBudget2;
    }
    
    /**
     * 查找预算（月）是否存在
     */
    public List<PutInPlanMonth> findPutMonthByNowTotalPutInAndType(PutInPlanMonth putInPlanMonth){
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("sourceId", putInPlanMonth.getSourceId());//年度ID
        param.put("resourceType", putInPlanMonth.getResourceType());
        param.put("promotionType", putInPlanMonth.getPromotionType());
        List<PutInPlanMonth> list = this.getSqlMapClientTemplate().queryForList(FIND_PutMonthByNowTotalPutInAndType,param);
        return list;
    }
}
